export interface ICommandBuilder {
  build(): Command;
}
export class Command {
  constructor(
    public executable: string,
    public command: string,
    public options: { [key: string]: string },
    public args: string,
    public fullCommand: string
  ) {

  }
}
// 符合 GNU 标准的命令行的组成格式 => https://www.jianshu.com/p/0a61481087dc
export class CommandBuilder implements ICommandBuilder {

  private _executable: string; // git xxx.exe  python index.py
  private _command: string; // push
  private _options: { [key: string]: string }; // --force
  private _arguments: string; // origin main

  setExecutable(executable: string) {
    this._executable = executable;
    return this;
  }

  setCommand(command: string) {
    this._command = command;
    return this;
  }

  setOptions(options: { [key: string]: string }) {

    this._options = options;
    return this;
  }

  setArgs(...args: string[]) {
    this._arguments = args.join(' ');
    return this;
  }

  build() {
    if (!this._executable) {
      throw new Error('🔥 executable must be passed in.');
    }

    let fullCommand = this._executable;

    if (this._command) {
      fullCommand += ` ${this._command}`;
    }

    if (this._options) {
      for (const [flag, value] of Object.entries(this._options)) {
        fullCommand += ` ${flag} ${value}`;
      }
    }

    if (this._arguments) {
      fullCommand += ` ${this._arguments}`;
    }

    return new Command(this._executable, this._command, this._options, this._arguments, fullCommand);
  }
}